import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_hx/exception/auth_exception.dart';
import 'package:flutter_hx/services/user_service.dart';
import '../../exception/data_converted_exception.dart';
import '../../exception/servers_interior_exception.dart';
import '../../model/BaseResult.dart';

class ResponseIntercept  extends Interceptor {

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    try{
      BaseResult baseResult=BaseResult.fromJson(response.data);
      response.data=baseResult.data;
      if(!baseResult.isSuccess){
        handler.reject(DioError(requestOptions: response.requestOptions,type: DioErrorType.response,error:  ServersInteriorException(error: baseResult.msg)));
        return;
      }
      super.onResponse(response, handler);
    }catch(e){
      handler.reject(DioError(requestOptions: response.requestOptions,type: DioErrorType.response,error:const DataConvertedException()));
    }

  }
}